package com.dong.service.Bill;

import com.dong.dao.BaseDao;
import com.dong.dao.Bill.BillDao;
import com.dong.dao.Bill.BillDaoImpl;
import com.dong.dao.Provider.ProviderDao;
import com.dong.dao.Provider.ProviderDaoImpl;
import com.dong.pojo.Bill;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BillServiceImpl implements BillService{
    private BillDao billDao;
    public BillServiceImpl() {
        billDao = new BillDaoImpl();
    }
    public boolean add(Bill bill) {
        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);
            int updateRows = billDao.add(connection, bill);
            if(updateRows > 0){
                flag = true;
                System.out.println("add success!");
            }else{
                System.out.println("add failed!");
            }
            connection.commit();
        } catch (Exception e) {
            try {
                System.out.println("rollback========================");
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }
    //通过查询条件获取供应商列表-模糊查询
    public List<Bill> getBillList(Bill bill) {
        Connection connection = null;
        List<Bill> billList = null;
        System.out.println("query productName ---- > " + bill.getProductName());
        System.out.println("query providerId ---- > " + bill.getProviderId());
        System.out.println("query isPayment ---- > " + bill.getIsPayment());
        connection = BaseDao.getConnection();
        try {
            billList = billDao.getBillList(connection,bill);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return billList;
    }
    //通过billId获取Bill
    public Bill getBillById(String id) {
        Connection connection = null;
        Bill bill = null;
        try {
            connection = BaseDao.getConnection();
            bill = billDao.getBillById(connection, id);
        } catch (Exception e) {
            e.printStackTrace();
            bill = null;
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return bill;
    }

    public boolean modify(Bill bill){
        boolean flag = true;
        Connection connection = null;

        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);
            int updateRows = billDao.modify(connection, bill);
            connection.commit();
            if(updateRows > 0){
                flag = true;
                System.out.println("modify success!");
            }else{
                System.out.println("modify failed!");
            }
        } catch (Exception e) {
            try {
                System.out.println("rollback================");
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }
    //删除订单信息
    public boolean deleteBillById(String delId) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);
            int updateRows = billDao.deleteBillById(connection, delId);
            connection.commit();
            if(updateRows > 0){
                flag = true;
                System.out.println("delete success!");
            }else{
                System.out.println("delete failed!");
            }
        } catch (Exception e) {
            try {
                System.out.println("rollback=========");
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }
}
